package com.huarui.market.action;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.action.BaseAction;
import com.huarui.market.model.Market;
import com.huarui.market.service.MarketService;
import com.huarui.params.Params;

public class MarketAction extends BaseAction{

	private static final long serialVersionUID = -5886363138921747695L;
	private static final Logger log = Logger.getLogger(MarketAction.class);
	private Map<String, Object> mesMap;
	
	private String queryParam;
	
	private Integer start;
	
	private MarketService marketService;
	
	

	public Integer getStart() {
		return start;
	}


	public void setStart(Integer start) {
		this.start = start;
	}


	public Map<String, Object> getMesMap() {
		return mesMap;
	}


	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}


	public String getQueryParam() {
		return queryParam;
	}


	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}


	public void setMarketService(MarketService marketService) {
		this.marketService = marketService;
	}

	/**
	 * 后台查询
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listMarket() {
		Map<String, Object> paramsMap = null;
		if(queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = this.marketService.getMarketListByComplex(paramsMap);
		return SUCCESS;
	}

	public String getMarketCombox(){
		
		Map<String, Object>	paramsMap = new HashMap<String, Object>();
		
		if(StringUtils.isBlank(queryParam)){
			
			return null;
			
		}
		
		 Integer companyType = Integer.valueOf(queryParam);
		
		
		if(companyType==2||companyType==3){
			
			
			paramsMap.put("marketType",0);
			
		}else if(companyType==4||companyType==5){
			
			
			paramsMap.put("marketType",1);
			
		}else{
			
		}
		mesMap= this.marketService.getMarketComBox(paramsMap);
		
		return SUCCESS;
	}
	
	/**
	 * 查询所有的二级市场及该公司的主市场
	 * @return
	 */
	public String selectMarket4RepairStore(){
		mesMap = new HashMap<String, Object>();
		try {
			mesMap = marketService.selectLevel2Market4RepairStore(Market.MARKET_TYPE_2_LEVEL);
		} catch (Exception e) {
			log.error("加载市场列表失败....",e);
			mesMap.put("success", false);
			mesMap.put("msg","加载市场列表失败.....");
		}
		 return SUCCESS;
	}
}
